package com.zhongen.oa.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongen.common.security.service.PigUser;
import com.zhongen.common.security.util.SecurityUtils;
import com.zhongen.oa.entity.NotifyAnnouncementEntity;
import com.zhongen.oa.entity.dto.NotifyAnnouncementDto;
import com.zhongen.oa.mapper.NotifyAnnouncementMapper;
import com.zhongen.oa.service.NotifyAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告信息
 *
 * @author Code Generator
 * @date 2024-02-27 09:48:24
 */
@Service
public class NotifyAnnouncementServiceImpl extends ServiceImpl<NotifyAnnouncementMapper, NotifyAnnouncementEntity>
		implements NotifyAnnouncementService {

	@Autowired
	NotifyAnnouncementMapper notifyAnnouncementMapper;
	@Autowired
	WxServlet wxServlet;

	@Override
	public IPage<NotifyAnnouncementDto> queryNotifyAnnouncementList(Page page,
			NotifyAnnouncementDto notifyAnnouncement) {
		return notifyAnnouncementMapper.queryNotifyAnnouncementList(page, notifyAnnouncement);
	}

	@Override
	public IPage<NotifyAnnouncementDto> queryNotifyAnnouncementByUser(Page page,
			NotifyAnnouncementDto notifyAnnouncement) {
		PigUser loginUser = SecurityUtils.getUser();
		List<Long> roles = SecurityUtils.getRoles();
		return notifyAnnouncementMapper.queryNotifyAnnouncementByUser(page, notifyAnnouncement, loginUser, roles);
	}

	@Override
	public boolean insertNotifyAnnouncement(NotifyAnnouncementDto notifyAnnouncement) {
		notifyAnnouncement.setDelFlag(0);
		int i = notifyAnnouncementMapper.insert(notifyAnnouncement);
		if (i > 0) {
			List<String> recipientCondition = notifyAnnouncement.getRecipientCondition();
			if (recipientCondition.size() > 0) {
				notifyAnnouncementMapper.insertAnnouncementDynamic(notifyAnnouncement.getId(),
						notifyAnnouncement.getRecipientFilterType(), recipientCondition);
			}
		}
		return i > 0;
	}

	@Override
	public boolean updateById(NotifyAnnouncementDto notifyAnnouncement) {
		notifyAnnouncementMapper.deleteByAnnouncementId(notifyAnnouncement.getId());
		List<String> recipientCondition = notifyAnnouncement.getRecipientCondition();
		if (recipientCondition.size() > 0) {
			notifyAnnouncementMapper.insertAnnouncementDynamic(notifyAnnouncement.getId(),
					notifyAnnouncement.getRecipientFilterType(), recipientCondition);
		}
		return notifyAnnouncementMapper.updateById(notifyAnnouncement) > 0;
	}

	@Override
	public void getNotifyHtml(Long id, HttpServletResponse response) {
		NotifyAnnouncementEntity notify = baseMapper.selectById(id);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> map = new HashMap<>();
		map.put("title", notify.getTitle());
		map.put("author", notify.getCreateBy());
		map.put("publishTime", LocalDateTimeUtil.format(notify.getCreateTime(), "yyyy-MM-dd"));
		map.put("content", notify.getContent());
		try {
			Configuration configuration = new Configuration();
			configuration.setClassForTemplateLoading(this.getClass(), "/notify");
			configuration.setDefaultEncoding("UTF-8");
			configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
			configuration.setLogTemplateExceptions(true);
			Template temp = configuration.getTemplate("announcement.html");
			StringWriter stringWriter = new StringWriter();
			temp.process(map, stringWriter);
			response.setHeader("Content-Type", " text/html;charset=UTF-8");
			response.getWriter().println(stringWriter);
		}
		catch (Exception e) {
			log.error("getNewsHtmlFail", e);
		}
	}

	@Override
	public IPage<NotifyAnnouncementDto> queryNotifyAnnouncementListApp(Page page,
			NotifyAnnouncementDto notifyAnnouncement) {
		PigUser user = SecurityUtils.getUser();
		List<Long> roles = SecurityUtils.getRoles();
		return notifyAnnouncementMapper.queryNotifyAnnouncementByUser(page, notifyAnnouncement, user, roles);
	}

	@Override
	public List<String> test() {
		List<String> list = new ArrayList<>();
		try {
			list = wxServlet.testSendMessage();
		} catch (IOException e) {
			System.err.println("发送失败");
		}
		return list;

	}

}
